<?php
namespace Application\Repository;
use Doctrine\ORM\EntityRepository;

class FriendRepository extends AbstractRepository
{
    protected $entity="Application\\Entity\\Friend";
    

    public function listByUser($userId, $offset=0, $limit = 0, $confirmed=-1, $toId = null)
    {
    	$dql = "SELECT c FROM $this->entity c";
    	$where = "";
    	if($userId!=null && $userId>0){
    	    $where .= ($where=="")?" WHERE":" AND";
    	    $where .= " IDENTITY(c.userFrom)= :user_from";
    	}
    	if($toId!=null && $toId>0){
    	    $where .= ($where=="")?" WHERE":" AND";
    	    $where .= " IDENTITY(c.userTo)= :user_to";
    	}
    	$dql .= $where;
    	if($confirmed>=0) $dql .= (($where=="")?" WHERE":" AND")." c.confirmed = ".((int)$confirmed);
    	//var_dump($confirmed);
    	//var_dump($dql);
    	//var_dump($userId);
    	//var_dump($toId);
    	 
    	$query = $this->getEntityManager()->createQuery($dql);
    	$query->setFirstResult($offset);
    	if($userId!=null && $userId>0){
    		$query->setParameter("user_from", $userId);
    	}
    	if($toId!=null && $toId>0){
    		$query->setParameter("user_to", $toId);
    	}
    	if($limit!=null && $limit>0) $query->setMaxResults($limit);
    	return $query->getResult();
    }

    public function listByUserSauv($userId, $offset=0, $limit = 0, $confirmed=-1, $toId = null)
    {
    	$dql = "SELECT c FROM $this->entity c WHERE IDENTITY(c.userFrom)=?1";
    	if($toId!=null && $toId>0){
    	    $dql .= " AND IDENTITY(c.userTo) =?2";
    	}
    	if($confirmed!=null && $confirmed>=0) $dql .= " AND c.confirmed = ".((int)$confirmed);
        $query = $this->getEntityManager()->createQuery($dql);
        $query->setFirstResult($offset);
        $query->setParameter(1, $userId);
    	if($toId!=null && $toId>0){
    	    $query->setParameter(2, $toId);
    	}
        if($limit!=null && $limit>0) $query->setMaxResults($limit);
    	return $query->getResult();
    }
    
    public function getByUser($userId,$number = 5000)
    {
        $dql = "SELECT c FROM $this->entity c WHERE IDENTITY(c.userFrom)=?1 OR IDENTITY(c.userTo)=?2";

        return $this->getEntityManager()->createQuery($dql)
                             ->setParameter(1, $userId)
                             ->setParameter(2, $userId)
                             ->setMaxResults($number)
                             ->getResult();
    }
    
    public function getByUserTo($userToId,$number = 5000)
    {
    	$dql = "SELECT c FROM $this->entity c WHERE IDENTITY(c.userTo)=?1";
    
    	return $this->getEntityManager()->createQuery($dql)
    	->setParameter(1, $userToId)
    	->setMaxResults($number)
    	->getResult();
    }
    
    public function getByUserFrom($userFormId,$number = 5000)
    {
        $dql = "SELECT c FROM $this->entity c WHERE IDENTITY(c.userFrom)=?1";

        return $this->getEntityManager()->createQuery($dql)
                             ->setParameter(1, $userFormId)
                             ->setMaxResults($number)
                             ->getResult();
    }
}